package com.yuandengta.yomall.cart.controller;

import com.yuandengta.yomall.cart.service.CartService;
import com.yuandengta.yomall.cart.vo.CartItemVO;
import com.yuandengta.yomall.cart.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * 购物车 {@link CartController}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 获取用户的所有购物项
     * @return
     */
    @ResponseBody
    @GetMapping("/cartItems")
    public List<CartItemVO> getCartItems(){
        return cartService.getUserCartItems();
    }

    /**
     * 添加商品到购物车
     * @param skuId 商品id
     * @param num 商品数量
     * @param redirectAttributes
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes){
        try {
            cartService.addToCart(skuId, num);
            redirectAttributes.addAttribute("skuId", skuId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 解决购物车重复提交问题
        return "redirect:http://cart.yomall.com/success.html";
    }

    /**
     * 跳转到商品添加成功页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/success.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVO cartItemVO = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemVO);
        return "success";
    }

    /**
     * 购物车列表页面
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model){
        try {
            CartVO cartVO = cartService.getCart();
            model.addAttribute("cart", cartVO);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "cartList";
    }

    /**
     * 勾选购物项
     * @param isChecked
     * @param skuId
     * @return
     */
    @GetMapping("/checkCart")
    public String checkCart(@RequestParam("isChecked") Integer isChecked,
                            @RequestParam("skuId") Long skuId){
        cartService.checkCart(skuId, isChecked);
        return "redirect:http://cart.yomall.com/cart.html";
    }

    /**
     * 修改购物项数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/changeCount")
    public String changeItemCount(@RequestParam("skuId") Long skuId,
                                  @RequestParam("num") Integer num){
        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.yomall.com/cart.html";
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.yomall.com/cart.html";
    }
}
